<cfscript>    
    view = createObject("component", session.rootDir & ".views.layout");
    if( Session.Auth EQ True and (Session.strAuthType EQ Session.MANAGERS or Session.strAuthType EQ Session.IT or Session.strAuthType EQ Session.HR) ){
		view.init("Step 3: Scheduled Employee Termination", "",	"","Home",main, "Termination");
    }else{
        view.init("Denied","","","Home","Denied");
    }    
    view.html();
</cfscript>

<cffunction name="main" access="public" output="true" returntype="any">
    <cfscript>
        import models.*;
        KronosQueries = CreateObject("component", session.rootDir & ".DatabaseQueries.Kronos");        
        TermSchedule = ORMExecuteQuery("FROM TerminationSchedule WHERE TermDate <= '#DateFormat(now(), "MM/DD/YYYY")#' AND Complete = 0");
    </cfscript>
    
    <h1 class="title">Step 3: Scheduled Employee Termination</h1>
    <div class="entry">
        Scheduled task for terminating AD, Email, Home Folder, and UserAccount.<br /><br />
    </div>
    
    <cfset Summary = 'Scheduled Archive.  <BR/>Started:  #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR /><hr /><br />'  >
    
    <cftry>
    	<cfloop from="1" to="#ArrayLen(TermSchedule)#" index="i" >
	         <cfscript>
	            TermRequest = ORMExecuteQuery("FROM TerminationRequests WHERE TerminationRequestsID = #TermSchedule[i].getTermReqID()#");
	            StepActions = ORMExecuteQuery("FROM StepActions WHERE ActTimeStamp IS NULL AND StepID = 23 AND TermReqID = #TermSchedule[i].getTermReqID()#");
	            Employee = KronosQueries.funcGetAllEmployees( TermRequest[1].getRosettaID() );
	            t = CreateObject("component", session.RootDir & ".utilities.Termination").init(TermSchedule[i].getTermReqID(), 0);
	            if( ArrayLen(TermSchedule) ){
	               t.ADDisable();
	               t.HomeFolderArchive();
	               t.EmailExport();
	               t.UserAccountDisable();
	               t.RosettaDisable();               
	               ts = entityLoadByPK("TerminationSchedule", TermSchedule[i].getTermScheduleID() );
                   ts.setComplete( 1 );
                   entitySave(ts);
	               Summary &= 'User:  #Employee.FirstName# #Employee.LastName# (#Employee.UserAccount#)<BR/> Started: #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR/><BR/>';
	            }            
	        </cfscript>        
	    </cfloop>
        
    <cfcatch type="Any" >
        <cfmail to=#Session.ITS_EMAIL# From="EPAR_Scheduled@scf.cc" subject="EPAR Scheduled Termination Error" type="html">
            <cfdump var="#CFCATCH#">
        </cfmail>
    </cfcatch>
    </cftry>

    <cfoutput>#Summary#</cfoutput>
    <cfmail to=#Session.ITS_EMAIL# From="EPAR_Scheduled@scf.cc" subject="EPAR Scheduled Termination Summary" type="html">
        #Summary#
    </cfmail>
</cffunction>